<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
class Mastervideo1 extends Common
{
    public function index(){
        $mastervideo=Db::query("select mv_id,mv_name,mv_intro,m_name,mv_video,mv_hit,mv_pic from mastervideos mv,masters m where mv.m_id=m.m_id");
        $this->assign("mastervideo",$mastervideo);
        return $this->fetch();
    }
    public function form(){

        return $this->fetch();
    }
    public function add(){
        //处理上传
        $file = request()->file('mv_pic');
        $mv_pic="";
//移动到框架应用根目录/public/uploads/目录下
        if($file){
            $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'admin'.DS.'images');

            if($info){
                $mv_pic=$info->getFilename();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }

//获取表单数据
        $data=[
            'mv_name'=>input('mv_name'),
            'mv_intro'=>input('mv_intro'),
           'm_id'=>input('m_id'),
            'mv_video'=>input('mv_video'),
            'mv_hit'=>input('mv_hit'),
            'mv_pic'=>empty($mv_pic)?"1.png":$mv_pic,
        ];
        if (Db::name('mastervideos')->insert($data)){
            $this->success('添加成功','mastervideo1/index');
        }else $this->error('添加失败');
        return $this->fetch();
    }

    public function mod($id){
        $mastervideo=Db::name("mastervideos")->find($id);
        $this->assign("mastervideo",$mastervideo);
       // dump($mastervideo);exit;
        return $this->fetch();

    }
    public function edit($id){
        //图片是否修改
        //获取其他的表单数据
       $mv_id=$id;
        //在数据库里修改
        $ccc=input('mv_pic');
        if(!$ccc){
            $ccc1=input('mv_pic1');
        }

        $file = request()->file('mv_pic');
        $mv_pic = "";
        //dump($uid);
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            //dump($file);
            $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'admin'. DS . 'images');
            // ;exit;
            if($info){
                $ccc1=$info->getFileName();
                //dump($pic);
            }else{
                // 上传失败获取错误信息  //
                echo $file->getError();
            }
        }

        $data=[

            'mv_name'=>input('mv_name'),
            'mv_intro'=>input('mv_intro'),
            'm_id'=>input('m_id'),
            'mv_video'=>input('mv_video'),
            'mv_hit'=>input('mv_hit'),
            'mv_pic'=>$ccc1,
        ];


        //dump($activity_id);
      //dump($data);

  // if(!empty($pic)){
        //  if(file_exists(ROOT_PATH . 'public' . DS . 'static'. DS . 'school'.DS.$pic))
        //      unlink(ROOT_PATH . 'public' . DS . 'static'. DS . 'school'.DS.$pic2);
        // }

        //存入数据库
     if(DB::name('mastervideos')->where('mv_id',$mv_id)->update($data)){

            $this->success('修改成功','admin/mastervideo1/index');

        }else{
            $this->error('修改失败');
        }
    }



    public function delete($id){
        if (Db::name("mastervideos")->delete($id)){
            $this->success('删除成功');
        }else $this->error('删除失败');
    }

    public function search(){
        $key=input('key');
        // dump($key);exit;
        if(!$key){
            $this->success('无搜索内容','Mastervideo1/index');
        }else{
            $mastervideo=Db::table('Mastervideos')->where('mv_name','like','%'.$key.'%')->select();
            $this->assign("mastervideo",$mastervideo);
            // dump($activity);exit;
            return $this->fetch('index');

        }

        // dump($sql);
    }
    public function jiekou(){

        $rs=Db::name('Mastervideos')->select();
        return json($rs);
    }


}